﻿window.Vt = function (module, postfix) {
    postfix = postfix || ".js";
    if (module.indexOf("&flush") > 0) {
        return TcmBaseURL + "/Server/" + module.replace(/&flush/g, "") + postfix + "?vt=" + Math.random();
    }
    return TcmBaseURL + "/Server/" + module + postfix + "?vt=" + _vt;
}
require.config({
    paths: {
        'vendor': Vt('lib/vendor'),
        'editor': Vt('lib/editor'),
        'sysconfig': TcmBaseURL + '/plus/adp/main/index?vt=' + _vt
    },
    waitSeconds: 0,
    map: {
        '*': {
            'css': TcmBaseURL + '/Server/lib/css.min.js'
        }
    }
});

window.addEventListener("popstate", (win, event) => {
    App.HistoryBack = true;
});

require(['css!lib/element-ui.css', 'css!lib/vendor.css', 'css!lib/ckeditor.css', 'css!adp/assets/fonts/style.css']);

define("vue", ["vendor"], function (vendor) {
    return vendor.Vue;
});
define("vue-router", ["vendor"], function (vendor) {
    return vendor.VueRouter;
});
define("element-ui", ["vendor"], function (vendor) {
    return vendor.ElementUI;
});
define("axios", ["vendor"], function (vendor) {
    return vendor.Axios;
});
define("qs", ["vendor"], function (vendor) {
    return vendor.Qs;
});
define("v-tooltip", ["vendor"], function (vendor) {
    return vendor.VTooltip;
});
function InitBrace(callback) {
    require([
        Vt("lib/brace/ext/language_tools"),
        Vt("lib/brace/mode/html"),
        Vt("lib/brace/mode/javascript"),
        Vt("lib/brace/mode/razor"),
        Vt("lib/brace/mode/css"),
        Vt("lib/brace/mode/text"),
        Vt("lib/brace/theme/chrome"),
        Vt("lib/brace/snippets/javascript")], callback);
}

require(['vue', 'vue-router', 'element-ui', 'v-tooltip'], function (Vue, Router, ElementUI, VTooltip) {

    Vue.use(Router);

    Vue.use(ElementUI);

    Vue.use(VTooltip);

    Vue.directive('validate', {
        bind: function (el, binding) {
            console.log('bind:', binding.value);
        },
        inserted: function (el, binding) {
            console.log('insert:', binding.value);
        },
        update: function (el, binding, vnode, oldVnode) {
            el.focus();
            console.log(el.dataset.name);//这里的数据是可以动态绑定的
            console.table({
                name: binding.name,
                value: binding.value,
                oldValue: binding.oldValue,
                expression: binding.expression,
                arg: binding.arg,
                modifiers: binding.modifiers,
                vnode: vnode,
                oldVnode: oldVnode
            });
        },
        componentUpdated: function (el, binding) {
            console.log('componentUpdated:', binding.name);
        }
    })

    window.App = new Vue();

    var router = new Router();

    router.beforeEach(function (to, from, next) {
        var r = App.Fn.parsePath(to.path);
        if (r.path === "/" && to.params.query && App.homePage && !r.query.autoSelect) {
            next({ path: App.homePage });
            return;
        }
        next();
    });

    require([Vt("adp/adp"), 'sysconfig', 'editor', Vt("lib/base64")], function (adp) {

        App.LogoUrl = TcmBaseURL + App.TcmConfig.platform.LogoUrl;
        var cc = 0;
        var rs = [];
        var ps = App.TcmConfig.platform.LoadPlus;
        for (var i = 0; i < ps.length; i++) {
            var plus = ps[i];
            require([Vt(plus + "/" + plus), 'css!' + Vt(plus + '/assets/css/' + plus, '.css')], function (main) {
                cc++;
                rs = rs.concat(Array.isArray(main) ? main : main.router);
                if (ps.length == cc) {
                    var v = new Vue({
                        el: '#app',
                        router: router,
                        render: function (h) { return h(adp.App); }
                    });
                    v.$router.addRoutes(rs);
                }
            });
        }
        App.homePage = App.TcmConfig.platform.HomePage;
    });
});

